<?php

/*
 * This file is part of the gedongdong/laravel_rbac_permission.
 *
 * (c) gedongdong <gedongdong2010@163.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\UserAmountLog;
use App\Http\Models\Users;
use App\Http\Models\ZhyReport;
use Illuminate\Http\Request;
use App\Library\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Validator;
use App\Validate\ReportUpdateValidate;

class ReportController extends Controller
{

    /**
     * 报告列表
     */
    public function index(Request $request)
    {

        $keywords = $request->get('keywords') ?? '';
        $reportLists = ZhyReport::orderBy('id','desc')->paginate(config('page_size'));

        return view('admin.report.index', ['reportLists' => $reportLists,'keywords'=>$keywords]);

    }

    //添加页
    public function add(Request $request)
    {
        return view('admin.report.add');
    }

   
    public function create(Request $request)
    {
        $validate = new ReportUpdateValidate($request);
        if (!$validate->goCheck()) {
            return Response::response(['code' => Response::PARAM_ERROR, 'msg' => $validate->errors->first()]);
        }

        $input = $request->all();
  
        try {
            
            $imageName = md5(uniqid());

            $clientArr = explode(',',$input['client']);

            $s = encrypt($input['number'].'#'.$clientArr[0]);
            $url = env('APP_URL').'/wx-help?s='.$s;

            $qrcode_image = createQrcode($url,'/images/report/',$imageName);
            $insertArr = [
                'number'          => $input['number'],
                'qrcode_image'    => $qrcode_image,
                'position'        => $input['position'],
                'client'          => $input['client'],
                'total_price'     => $input['total_price'],
                'price'           => $input['price'],
                'built_in'        => $input['built_in'],
                'property_nature' => $input['property_nature'],
                'is_change'       => $input['is_change'],
                'created_at'      => date('Y-m-d H:i:s',time()),
                'updated_at'      => date('Y-m-d H:i:s',time()),
            ];
            
            ZhyReport::insert($insertArr);

            return Response::response();
        } catch (QueryException $e) {
            return Response::response(['code' => Response::SQL_ERROR, 'e' => $e]);
        }
    }

    //修改页
    public function edit(Request $request)
    {
        $report_id = $request->get('report_id');

        $error = '';
        $report = null;
        if (!$report_id) {
            $error = '参数有误';
        } else {
            $report = ZhyReport::find($report_id);
            if (!$report) {
                $error = '信息错误';
            }
        }

        return view('admin.report.edit', ['report' => $report, 'error' => $error]);
    }

    //修改
    public function update(Request $request)
    {
        $validate = new ReportUpdateValidate($request);
        if (!$validate->goCheck()) {
            return Response::response(['code' => Response::PARAM_ERROR, 'msg' => $validate->errors->first()]);
        }

        $input = $request->all();

        if(empty($input['id'])){
            return Response::response(['code' => 101, 'msg' => '参数有误']);
        }
  
        try {

            $report = ZhyReport::find($input['id']);

            $arr = pathinfo($report->qrcode_image);
            if(empty($arr['filename'])){
                $imageName = md5(uniqid());
            }else{
                $imageName = $arr['filename'];
            }

            $clientArr = explode(',',$input['client']);

            $s = encrypt($input['number'].'#'.$clientArr[0]);
            $url = env('APP_URL').'/wx-help?s='.$s;
            
            $qrcode_image = createQrcode($url,'/images/report/',$imageName);

            
            $report->number          = $input['number'];
            $report->qrcode_image    = $qrcode_image;
            $report->position        = $input['position'];
            $report->client          = $input['client'];
            $report->total_price     = $input['total_price'];
            $report->price           = $input['price'];
            $report->built_in        = $input['built_in'];
            $report->property_nature = $input['property_nature'];
            $report->is_change       = $input['is_change'];
            $report->save();

            return Response::response();
        } catch (QueryException $e) {
            return Response::response(['code' => Response::SQL_ERROR, 'e' => $e]);
        }
    }

    //删除
    public function delete(Request $request)
    {
        $input = $request->all();
        $validate = Validator::make($input, [
            'report_id' => 'required|numeric',       //id
        ]);
        if ($validate->fails()) {
            return Response::response(['code' => 101, 'msg' => '参数有误']);
        }

        try {
            ZhyReport::where('id',$input['report_id'])->delete();

            return Response::response();
        } catch (QueryException $e) {
            return Response::response(['code' => Response::SQL_ERROR, 'e' => $e]);
        }
    }

    

}
